package org.groupg.project.config;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@JacksonXmlRootElement(localName = "proxyConfig")
public class XmlProxyConfig {
    @JacksonXmlProperty(localName = "port")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<XmlPortConfig> ports;

    // 从ProxyConfig转换
    public static XmlProxyConfig fromProxyConfig(ProxyConfig config) {
        XmlProxyConfig xmlConfig = new XmlProxyConfig();
        xmlConfig.ports = config.getPortRules().entrySet().stream()
                .map(entry -> new XmlPortConfig(entry.getKey(), entry.getValue()))
                .collect(Collectors.toList());
        return xmlConfig;
    }

    // 转换为ProxyConfig
    public ProxyConfig toProxyConfig() {
        ProxyConfig config = new ProxyConfig();
        Map<Integer, List<RuleConfig>> portRules = ports.stream()
                .collect(Collectors.toMap(
                        XmlPortConfig::getNumber,
                        XmlPortConfig::getRules
                ));
        config.setPortRules(portRules);
        return config;
    }

    // Getters and Setters
    public List<XmlPortConfig> getPorts() {
        return ports;
    }

    public void setPorts(List<XmlPortConfig> ports) {
        this.ports = ports;
    }
}